package me.magicall.coll.bridge;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;

import me.magicall.coll.sorted.Sorted;


public class ListSet<E> extends AbstractList<E>//
		implements List<E>, Set<E>, SortedSet<E>, Serializable, Sorted {
	private static final long serialVersionUID = -7260955667013286199L;
	//------------------------------
	private final List<E> list;

	//------------------------------
	/**
	 * 这个list需要保证没有重复元素
	 * @param list
	 */
	public ListSet(List<E> list) {
		super();
		this.list = list;
	}

	public ListSet() {
		this(new ArrayList<E>());
	}

	//-------------------------------collection
	public String toString() {
		return list.toString();
	}

	@Override
	public Iterator<E> iterator() {
		return list.iterator();
	}

	@Override
	public int size() {
		return list.size();
	}

	public boolean add(E e) {
		return list.contains(e) ? false : list.add(e);
	}

	public boolean addAll(Collection<? extends E> c) {
		return super.addAll(c);
	}

	public void clear() {
		list.clear();
	}

	public boolean contains(Object o) {
		return list.contains(o);
	}

	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}

	public boolean equals(Object o) {
		return list.equals(o);
	}

	public int hashCode() {
		return list.hashCode();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public boolean remove(Object o) {
		return list.remove(o);
	}

	public boolean removeAll(Collection<?> c) {
		return list.removeAll(c);
	}

	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c);
	}

	public Object[] toArray() {
		return list.toArray();
	}

	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}

	//---------------------------list
	public void add(int index, E element) {
		super.add(index, element);
	}

	public boolean addAll(int index, Collection<? extends E> c) {
		return super.addAll(index, c);
	}

	public E get(int index) {
		return list.get(index);
	}

	public int indexOf(Object o) {
		return list.indexOf(o);
	}

	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}

	public ListIterator<E> listIterator() {
		return super.listIterator();
	}

	public ListIterator<E> listIterator(int index) {
		return super.listIterator(index);
	}

	public E remove(int index) {
		return list.remove(index);
	}

	public E set(int index, E element) {
		return list.set(index, element);
	}

	public List<E> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}

	//-----------------------------sorted set
	@Override
	public E first() {
		if (isEmpty()) {
			throw new NoSuchElementException();
		}
		return list.get(0);
	}

	@Override
	public SortedSet<E> headSet(E toElement) {
		final int index = i(toElement);
		if (index < 0) {
			throw new IllegalArgumentException();
		}
		return new ListSet<E>(list.subList(0, index));
	}

	@Override
	public E last() {
		if (isEmpty()) {
			throw new NoSuchElementException();
		}
		return list.get(list.size() - 1);
	}

	@Override
	public SortedSet<E> subSet(E fromElement, E toElement) {
		final int from = i(fromElement);
		if (from < 0) {
			throw new IllegalArgumentException();
		}
		final int to = i(toElement);
		if (to < 0) {
			throw new IllegalArgumentException();
		}
		return new ListSet<E>(list.subList(from, to));
	}

	@Override
	public SortedSet<E> tailSet(E fromElement) {
		final int from = i(fromElement);
		if (from < 0) {
			throw new IllegalArgumentException();
		}
		return new ListSet<E>(list.subList(from, list.size()));
	}

	@Override
	public Comparator<? super E> comparator() {
		return null;
	}

	private int i(E element) {
		return list.indexOf(element);
	}

}
